// 6.cpp

#include "exercise.h"


class scene6 : public exerciseScene
{
public:
    scene6();
    virtual void setupScene();
    virtual void cleanScene();
    virtual void renderScene();

    virtual void mouseDown(int button, GLint xViewPort, GLint yViewPort);
    virtual void mouseUp(int button, GLint xViewPort, GLint yViewPort);
    virtual void mouseMove(GLint xViewPort, GLint yViewPort);

    virtual void keyDown(int key);

    void drawCube();
private:
    gestureView m_view;
    int m_button;
};

scene6::scene6() :
    m_button(mouseButtonNone)
{

}

void scene6::setupScene()
{
    glextInitOpenglEntry12();

    //glClearColor(1.0f, 1.0f, 0.0f, 0.0f);

    //glBlendFunc(GL_ONE, GL_ONE);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glEnable(GL_BLEND);
}

void scene6::cleanScene()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glDisable(GL_BLEND);
}

void scene6::drawCube()
{
    GLfloat interwined[] = 
    {
        1.0f, 0.2f, 1.0f, 0.4f, -1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 0.2f, 0.4f, -1.0f, -1.0f, 1.0f,
        0.0f, 1.0f, 1.0f, 0.4f, 1.0f, -1.0f, 1.0f,
        0.0f, 0.2f, 1.0f, 0.4f, 1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 0.2f, 0.4f, -1.0f, 1.0f, -1.0f,
        1.0f, 1.0f, 1.0f, 0.4f, -1.0f, -1.0f, -1.0f,
        0.2f, 0.2f, 1.0f, 0.4f, 1.0f, -1.0f, -1.0f,
        1.0f, 1.0f, 0.2f, 0.4f, 1.0f, 1.0f, -1.0f
    };
    GLubyte frontIndices[] = {0, 1, 2, 3};
    GLubyte backIndices[] = {7, 6, 5, 4};
    GLubyte leftIndices[] = {4, 5, 1, 0};
    GLubyte rightIndices[] = {3, 2, 6, 7};
    GLubyte topIndices[] = {4, 0, 3, 7};
    GLubyte bottomIndices[] = {1, 5, 6, 2};

    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_FLOAT, 7*sizeof(GLfloat), &interwined[0]);
    glVertexPointer(3, GL_FLOAT, 7*sizeof(GLfloat), &interwined[4]);

    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, frontIndices);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, backIndices);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, leftIndices);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, rightIndices);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, topIndices);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottomIndices);
}

void scene6::renderScene()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glDepthMask(GL_FALSE);

    m_view.applyView();

    drawCube();
}

void scene6::mouseDown(int button, GLint xViewPort, GLint yViewPort)
{
    m_button = button;
    m_view.beginGesture(xViewPort, yViewPort);
}

void scene6::mouseUp(int button, GLint xViewPort, GLint yViewPort)
{
    m_button = mouseButtonNone;
}

void scene6::mouseMove(GLint xViewPort, GLint yViewPort)
{
    if (mouseButtonMiddle == m_button)
    {
        m_view.doGestureRotate(xViewPort, yViewPort);
        if (wnd)
            wnd->postRedisplay();
    }
}

void scene6::keyDown(int key)
{
    switch (key)
    {
    case 'a':
    case 'A':
        glBlendEquation(GL_FUNC_ADD);
        break;
    case 's':
    case 'S':
        glBlendEquation(GL_FUNC_SUBTRACT);
        break;
    case 'r':
    case 'R':
        glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
        break;
    case 'm':
    case 'M':
        glBlendEquation(GL_MIN);
        break;
    case 'x':
    case 'X':
        glBlendEquation(GL_MAX);
        break;
    }

    if (wnd)
        wnd->postRedisplay();
}

exerciseScene* createScene6()
{
    return new scene6;
}